﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using T.FAS.Runtime.Metadata.Entity;
using T.ServiceComponent.DAC.Service;
using T.ServiceComponent.DataAccessEx.Core;

namespace T.FAS.Runtime.Metadata.Dao
{
    public class MetaVisitHistoryDao : DaoService<MetaDataVisitHistory>
    {
        public MetaVisitHistoryDao(string db = "TFAS") : base()
        {
            this.DBName = db;
        }

        public override string IDColumnName
        {
            get
            {
                return "ID";
            }
        }

        public override string TableName
        {
            get
            {
                return "Meta_VisitHistory";
            }
        }

        public void DeleteVisitHistory(MetaDataVisitHistory visitHistory)
        {

            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();

            paramsList.Add(this.DB.MakeInParam("@StartVisitTime", TDbDataType.DateTime, visitHistory.VisitTime.ToString("yyyy-MM-dd")));
            paramsList.Add(this.DB.MakeInParam("@EndVisitTime", TDbDataType.DateTime, visitHistory.VisitTime.ToString("yyyy-MM-dd 23:59:59")));
            paramsList.Add(this.DB.MakeInParam("@MetaDataID", TDbDataType.VarChar, visitHistory.MetaDataID));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, visitHistory.ProjectBranchID));
            paramsList.Add(this.DB.MakeInParam("@UserID", TDbDataType.VarChar, visitHistory.UserID));
            string sql = "Delete From Meta_VisitHistory where VisitTime>@StartVisitTime and VisitTime<@EndVisitTime and MetaDataID=@MetaDataID and ProjectBranchID=@ProjectBranchID and UserID=@UserID";
            this.DB.ExecSqlStatement(sql, paramsList.ToArray());
        }

        public int ClearHistoryVisitHistory(int beforeDay) {
            if (beforeDay > 0) return 0;
            var dt = DateTime.Now.AddDays(beforeDay);
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@VisitTime", TDbDataType.DateTime, dt));
            string sql = "Delete From Meta_VisitHistory where VisitTime<@VisitTime";
            return this.DB.ExecSqlStatement(sql, paramsList.ToArray());
        }

        public int DeleteHistoryVisitHistory(string userID, string projectBranchID) {

            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            paramsList.Add(this.DB.MakeInParam("@UserID", TDbDataType.VarChar, userID));
            string sql = "Delete From Meta_VisitHistory where UserID=@UserID AND ProjectBranchID=@ProjectBranchID";
            return this.DB.ExecSqlStatement(sql, paramsList.ToArray());
        }

        public List<MetaDataVisitHistory> GetMetaDataVisitHistories(string userID, string projectBranchID, string visitTime)
        {
            string sql = "select vh.ID,vh.ProjectBranchID,MetaDataID,meta.Code MetaDataCode,meta.Name MetaDataName,meta.MetaTypeID,UserID,UserName,VisitTime,vh.Creator,vh.CreateTime,vh.LastModifier,vh.LastModifyTime from Meta_VisitHistory vh inner join meta_basicinfo meta on vh.MetaDataID = meta.ID and vh.ProjectBranchID = meta.ProjectBranchID";
            string sqlCondition = "vh.MetaTypeID<>'Project' AND UserID=@UserID AND vh.ProjectBranchID=@ProjectBranchID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@UserID", TDbDataType.VarChar, userID));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            if (string.IsNullOrEmpty(visitTime) == false) {
                sqlCondition = string.Format("{0} AND VisitTime<@StartVisitTime  AND VisitTime>@ENDVisitTime", sqlCondition);
                paramsList.Add(this.DB.MakeInParam("@StartVisitTime", TDbDataType.DateTime, DateTime.Parse(visitTime)));
                paramsList.Add(this.DB.MakeInParam("@ENDVisitTime", TDbDataType.DateTime, DateTime.Parse(visitTime+" 23:59:59")));
            }
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1} Order By VisitTime DESC", sql,sqlCondition), paramsList.ToArray());
            List<MetaDataVisitHistory> list = new List<MetaDataVisitHistory>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0) {
                foreach(DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(ConvertToObject(this.Columns, row));
                }
            }
            return list;

        }
        public int GetMetaDataVisitHistoryCount(string userID, string projectBranchID) {
            string sql = "select count(1) total from Meta_VisitHistory vh inner join meta_basicinfo meta on vh.MetaDataID = meta.ID and vh.ProjectBranchID = meta.ProjectBranchID";
            string sqlCondition = "vh.MetaTypeID<>'Project' AND UserID=@UserID AND vh.ProjectBranchID=@ProjectBranchID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@UserID", TDbDataType.VarChar, userID));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));

            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1}", sql, sqlCondition), paramsList.ToArray());
            int total = 0;
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                total = Convert.ToInt32(ds.Tables[0].Rows[0]["total"]);
            }
            return total;
        }
        public List<MetaDataVisitHistory> GetMetaDataVisitHistories(string userID, string projectBranchID, int rows, int page)
        {
            string sql = "select vh.ID,vh.ProjectBranchID,MetaDataID,meta.Code MetaDataCode,meta.Name MetaDataName,meta.MetaTypeID,UserID,UserName,VisitTime,vh.Creator,vh.CreateTime,vh.LastModifier,vh.LastModifyTime from Meta_VisitHistory vh inner join meta_basicinfo meta on vh.MetaDataID = meta.ID and vh.ProjectBranchID = meta.ProjectBranchID";
            string sqlCondition = "vh.MetaTypeID<>'Project' AND UserID=@UserID AND vh.ProjectBranchID=@ProjectBranchID";
            List<IDbDataParameter> paramsList = new List<IDbDataParameter>();
            paramsList.Add(this.DB.MakeInParam("@UserID", TDbDataType.VarChar, userID));
            paramsList.Add(this.DB.MakeInParam("@ProjectBranchID", TDbDataType.VarChar, projectBranchID));
            
            var ds = this.DB.ExecuteDataSet(string.Format("{0} WHERE {1} Order By VisitTime DESC limit {2} offset {3}", sql, sqlCondition, rows, (page - 1) * rows), paramsList.ToArray());
            List<MetaDataVisitHistory> list = new List<MetaDataVisitHistory>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    list.Add(ConvertToObject(this.Columns, row));
                }
            }
            return list;

        }

    }
}
